package com.imyours.webservice.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.imyours.message.MsgCenter;
import com.imyours.model.message.MessageEntity;
import com.imyours.model.user.UserEntity;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

/**
 * 获取信息
 * 
 * @author SLevenc
 * 
 */
@Service(name = "getMsg")
public class GetMessage extends ContextService {

	public GetMessage(Context context) {
		super(context);
	}

	public void execute() {
		UserEntity user = getUserEntity();
		if (user != null) {
			List<MessageEntity> msgList = MsgCenter.getInstance().getMessage(user);
			List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Map<String,String> msgMap = null;
			for(int i = 0 ; i <msgList.size();i++){
				msgMap = new HashMap<String, String>();
				msgMap.put("sender", ""+msgList.get(i).getSender().getId());
				msgMap.put("sendDate", sdf.format(msgList.get(i).getSendDate()));
				msgMap.put("body", msgList.get(i).getContent());
				resultList.add(msgMap);
			}
			context.putResult("msg", resultList);
			context.success();
		}
	}

}
